package com.gkzf.ai.module.crm.controller.admin.creditReport;

import com.gkzf.ai.framework.common.pojo.CommonResult;
import com.gkzf.ai.framework.common.pojo.PageResult;
import com.gkzf.ai.framework.web.core.util.WebFrameworkUtils;
import com.gkzf.ai.module.crm.config.NoRepeatSubmit;
import com.gkzf.ai.module.crm.controller.admin.creditReport.vo.creditreportbasic.CreditReportBasicPageReqVO;
import com.gkzf.ai.module.crm.controller.admin.creditReport.vo.creditreportbasic.CreditReportBasicRespVO;
import com.gkzf.ai.module.crm.controller.admin.creditReport.vo.creditreportbasic.CreditReportBasicSaveReqVO;
import com.gkzf.ai.module.crm.dal.dataobject.creditReport.CreditReportBasicDO;
import com.gkzf.ai.module.crm.service.creditReport.CreditReportAdmittanceConditionService;
import com.gkzf.ai.module.crm.service.creditReport.CreditReportBasicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.gkzf.ai.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - CRM 征信报告基础")
@RestController
@RequestMapping("/crm/credit-report-basic")
@Validated
public class CreditReportBasicController {

    @Resource
    private CreditReportBasicService creditReportBasicService;

    @Resource
    private CreditReportAdmittanceConditionService conditionService;




    @PostMapping("/create")
    @Operation(summary = "创建CRM 征信报告基础")
    @NoRepeatSubmit(value = "上传征信")
    @PreAuthorize("@ss.hasPermission('crm:creditReportBasic:create')")
    public CommonResult<Long> createCreditReportBasic(@Valid @RequestBody CreditReportBasicSaveReqVO creditReportBasicSaveReqVO) {
        String channelType = creditReportBasicSaveReqVO.getChannelType();
        CreditReportBasicDO creditReportBasic = creditReportBasicService.createCreditReportBasic(creditReportBasicSaveReqVO, WebFrameworkUtils.getLoginUserId());
        conditionService.createCreditReportAdmittanceCondition(creditReportBasic.getId());
        creditReportBasicService.createCreditReport(creditReportBasic,channelType,WebFrameworkUtils.getLoginUserId());
        return success(1L);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除CRM 征信报告基础")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:creditReportBasic:delete')")
    public CommonResult<Boolean> deleteCreditReportBasic(@RequestParam("id") Long id) {
        creditReportBasicService.deleteCreditReportBasic(id);
        return success(true);
    }

    @GetMapping("/matchProduct")
    @Operation(summary = "匹配CRM 匹配产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:creditReportDetails:matchProduct')")
    public CommonResult<Object> matchProduct(@RequestParam("id") Long id) {
        return creditReportBasicService.matchProduct(id);
    }


    @GetMapping("/page")
    @Operation(summary = "获得CRM 征信报告基础分页")
    @PreAuthorize("@ss.hasPermission('crm:creditReportBasic:query')")
    public CommonResult<PageResult<CreditReportBasicRespVO>> getCreditReportBasicPage(@Valid CreditReportBasicPageReqVO pageReqVO) {

        PageResult<CreditReportBasicRespVO> pageResult = creditReportBasicService.getCreditReportBasicPage(pageReqVO);
        return success(pageResult);
    }


}